classdef dotsPlayablePPATwoTones < dotsPlayablePPA
    % @class dotsPlayablePPATwoTones
    %
    % PPA for "PsychPortAudio"
    %   type 'help PsychPortAudio' for details
    % Play sounds using high-precision sound driver
    %   implemented in Psychtoolbox 3
    
    properties (SetObservable = true)
        
        % LEFT channel
        % duh, in cycles/s
        toneLFrequency;
        
        % double duh, in s
        toneLDuration;
        
        % RIGHT channel
        % duh, in cycles/s
        toneRFrequency;
        
        % double duh, in s
        toneRDuration;
    end
    
    properties (SetObservable = false)
        
        % returned by PsychPortAudio('FillBuffer');
        underflow;

        % returned by PsychPortAudio('FillBuffer');
        nextSampleStartIndex;

        % returned by PsychPortAudio('FillBuffer');
        nextSampleETASecs;

    end
    
    methods
        
        function self = dotsPlayablePPATwoTones
            self = self@dotsPlayablePPA;
            self.channels = 2;
        end
        
        function prepareToPlay(self)
            
            % for now just play together.. could conditionally
            %   play just L or R
            nCyclesL      = self.toneLFrequency * self.toneLDuration;
            nSamplesL     = self.sampleFrequency * self.toneLDuration;
            radsL         = linspace(0, nCyclesL*2*pi, nSamplesL);
            
            nCyclesR      = self.toneRFrequency * self.toneRDuration;
            nSamplesR     = self.sampleFrequency * self.toneRDuration;
            radsR         = linspace(0, nCyclesR*2*pi, nSamplesR);
            
            if nSamplesL > nSamplesR
                radsR = cat(2, radsR, zeros(nSamplesL - nSamplesR));
            elseif nSamplesR > nSamplesL
                radsL = cat(2, radsR, zeros(nSamplesR - nSamplesL));
            end
            self.waveform = sin([radsL; radsR]);
            
            [self.underflow,self.nextSampleStartIndex,self.nextSampleETASecs] = ...
                PsychPortAudio('FillBuffer', self.pahandle, ...
                self.waveform);
        end
    end
end